//
//  BottomControlView.m
//  BaChat
//
//  Created by 刘洋 on 16/6/17.
//  Copyright © 2016年 刘洋. All rights reserved.
//

#import "BottomControlView.h"

@interface BottomControlView ()




@end




@implementation BottomControlView


-(void)dealloc{
    Block_release(_faceButtonBlock);
    Block_release(_touchVoiceStartButtonBlock);
    Block_release(_touchVoiceFinishButtonBlock);
    Block_release(_textBlock);
    Block_release(_voiceBlock);
    [_addButton release];
    [_faceButton release];
    [_messageTextView release];
    [_messageTextField release];
    [_touchVoiceButton release];
    [super dealloc];
}






-(instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubviews];
    }
    return self;
    
    
}

-(void)createSubviews{
    
    //语音按钮
    BaseButton *soundsButton = [BaseButton buttonWithType:UIButtonTypeCustom];
    soundsButton.backgroundColor = [UIColor clearColor];
    
    [self addSubview:soundsButton];
    [soundsButton setBackgroundImage:[UIImage imageNamed:@"message_privatechat_voice_background_highlighted"] forState:UIControlStateNormal];
    [soundsButton addTarget:self action:@selector(soundsButton:) forControlEvents:UIControlEventTouchUpInside];
    [soundsButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
       
        make.left.mas_equalTo(5);
        make.bottom.mas_equalTo(-5);
        make.width.height.mas_equalTo(40);
        soundsButton.layer.masksToBounds = YES;
        soundsButton.layer.cornerRadius = 20;
        
    }];
    
    //输入框
    self.messageTextView = [[UITextView alloc]init];
    self.messageTextView.backgroundColor = [UIColor whiteColor];
    self.messageTextView.layer.borderWidth = 1;
    self.messageTextView.layer.borderColor = [RGBAColor(220, 220, 220) CGColor];
    self.messageTextView.font = [UIFont systemFontOfSize:17];
    self.messageTextView.layer.masksToBounds = YES;
    self.messageTextView.layer.cornerRadius = 5;
    [self addSubview:self.messageTextView];
    
    
    //换成发送按钮
    self.messageTextView.returnKeyType = UIReturnKeySend;
    
    [self.messageTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        NSLog(@"4441111111111");
        
        make.left.mas_equalTo(soundsButton.mas_right).offset(5);
        make.height.mas_equalTo(40);
        make.bottom.mas_equalTo(-5);
        make.right.mas_equalTo(-90);
        
        
    }];
    [_messageTextView release];
    
    
    
    
    
    
    
    
    //表情按钮
    self.faceButton = [BaseButton buttonWithType:UIButtonTypeCustom];
    self.faceButton.backgroundColor = [UIColor clearColor];
    [self addSubview:self.faceButton];
    [self.faceButton setImage:[[UIImage imageNamed:@"faceButton"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [self.faceButton addTarget:self action:@selector(faceButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.faceButton mas_makeConstraints:^(MASConstraintMaker *make) {
   
        make.right.mas_equalTo(-45);
        make.width.height.mas_equalTo(40);
        make.bottom.mas_equalTo(-5);
        self.faceButton.layer.masksToBounds = YES;
        self.faceButton.layer.cornerRadius = 20;
    }];
    
    //更多按钮
     self.addButton = [BaseButton buttonWithType:UIButtonTypeCustom];
    self.addButton.backgroundColor = [UIColor clearColor];
    [self addSubview:self.addButton];
    [self.addButton setBackgroundImage:[UIImage imageNamed:@"message_privatechat_add_background_highlighted"] forState:UIControlStateNormal];
    [self.addButton addTarget:self action:@selector(addButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.addButton mas_makeConstraints:^(MASConstraintMaker *make) {
    
        make.right.mas_equalTo(-5);
        make.width.height.mas_equalTo(40);
        make.bottom.mas_equalTo(-5);
        self.addButton.layer.masksToBounds = YES;
        self.addButton.layer.cornerRadius = 20;
        
    }];

   
    
    

}

#pragma mark -- 语音按钮
-(void)soundsButton:(BaseButton *)button{

    self.voiceBlock(button);
    
 
}


#pragma mark -- 表情按钮
-(void)faceButton:(BaseButton *)button{
    
    
    
    self.faceButtonBlock(button);
/*
    if (button.selected == NO) {
        [button setBackgroundImage:[UIImage imageNamed:@"message_keyboard_background"] forState:UIControlStateNormal];
        [button setImage:nil forState:UIControlStateNormal];
    }else{
        [button setBackgroundImage:nil forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"faceButton"] forState:UIControlStateNormal];
    }
    button.selected = !button.selected;
*/    
    
    
}

#pragma mark -- 更多按钮
-(void)addButton:(BaseButton *)button{
    
    
    self.morePlusButtonBlock(button);
 
}




#pragma mark -- 添加语音按钮
-(void)createTouchVoiceButton{
    
    self.touchVoiceButton = [BaseButton buttonWithType:UIButtonTypeCustom];
    self.touchVoiceButton.backgroundColor = [UIColor whiteColor];
    [self.touchVoiceButton setTitle:@"按住 说话" forState:UIControlStateNormal];
    [self.touchVoiceButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.touchVoiceButton.layer.borderWidth = 1;
    self.touchVoiceButton.layer.borderColor = [RGBAColor(220, 220, 220) CGColor];
    self.touchVoiceButton.layer.masksToBounds = YES;
    self.touchVoiceButton.layer.cornerRadius = 5;
    [self addSubview:self.touchVoiceButton];
    //开始录制语音
    [self.touchVoiceButton addTarget:self action:@selector(touchVoiceStartButton:) forControlEvents:UIControlEventTouchDown];
    //完成录制
    [self.touchVoiceButton addTarget:self action:@selector(touchVoiceFinishButton:) forControlEvents:UIControlEventTouchUpInside];
   
    
//  UIControlEventTouchDown不同的是按下后就触发，不受到后续Touch动作的影响，例如按下滑动，同样会触发事件
    
    
 
   
    
    
    
    [self.touchVoiceButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(50);
        make.height.mas_equalTo(40);
        make.bottom.mas_equalTo(-5);
        make.right.mas_equalTo(-90);
        
        
    }];

    
}

#pragma mark -- 开始录制语音的按钮
-(void)touchVoiceStartButton:(BaseButton *)button{
    
    self.touchVoiceStartButtonBlock(button);
    
}

#pragma mark -- 完成录制语音的按钮
-(void)touchVoiceFinishButton:(BaseButton *)button{
    
    self.touchVoiceFinishButtonBlock(button);
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
